//
//  FileReceive.m
//  FileReceive
//
//  Created by cr on 2021/3/24.
//

#import <Foundation/Foundation.h>
#import <React/RCTEventEmitter.h>
#import <AudioToolbox/AudioToolbox.h>
@interface FileReceive : RCTEventEmitter<RCTBridgeModule>

@end

@implementation FileReceive

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(playRingtone)
{
  AudioServicesPlaySystemSound(4095);
}
RCT_EXPORT_METHOD(checkFile){
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* url =  [defaults stringForKey:@"openurl"];
    NSURL* aURL = [NSURL URLWithString:url];
    if([[aURL scheme] isEqualToString:@"file"])
    {
        NSError *error = nil;
        NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:aURL.path error:&error];
        NSArray *array = [aURL.path componentsSeparatedByString:@"/"];
        NSString* name = [array lastObject];
        [self sendEventWithName:@"FileReceive"
                           body:@{@"filePath":aURL.absoluteString,@"code":@(0),
                                  @"name":name,
                                  @"mimeType":[[name componentsSeparatedByString:@"."] lastObject],
                                  @"size": [attributes objectForKey:NSFileSize]}];

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:@"" forKey:@"openurl"];
        [defaults synchronize];
    }

}
- (BOOL)handleOpenURL:(NSNotification *)aNotification
{
    NSString * aURLString =  [aNotification userInfo][@"url"];
    NSURL * aURL = [NSURL URLWithString:aURLString];
    if([[aURL scheme] isEqualToString:@"file"])
    {
        NSError *error = nil;
        NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:aURL.path error:&error];

        if (error) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:@"" forKey:@"openurl"];
        [defaults synchronize];
          return YES;
        }

        NSArray *array = [aURL.path componentsSeparatedByString:@"/"];
        NSString* name = [array lastObject];
        
        [self sendEventWithName:@"FileReceive"
                           body:@{@"filePath":aURL.absoluteString,@"code":@(1),
                                  @"name":name,
                                  @"mimeType":[[name componentsSeparatedByString:@"."] lastObject],
                                  @"size": [attributes objectForKey:NSFileSize]}];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:@"" forKey:@"openurl"];
        [defaults synchronize];
        return  YES;
    }
    return  NO;
}
+(BOOL)requiresMainQueueSetup
{
    return YES;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOpenURL:) name:@"RCTOpenURLNotification" object:nil];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (NSArray<NSString *> *)supportedEvents {
    return @[@"FileReceive"];
}


@end
